import { defineStore } from 'pinia'
import type { StoreDefinition } from 'pinia'
import { ILoginStore } from './type'

import router from '@/router'
import { login } from '@/api/login'


export const useLoginStore: StoreDefinition<string, ILoginStore> = defineStore('login', {
  state: () => {
    return {
      username: 'Ares',
      password: ''
    }
  },
  actions: {
    async loginAction(password: string) {
      this.password = password
      const result = await login({ username: this.username, password })
      if (result.flag) {
        router.push('/home')
      }
    }
  }
})
